package com.zs.leymovie.activity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.VideoView;

import com.zs.leymovie.R;


public class VideoViewActivity extends AppCompatActivity {

    private VideoView videoView;

    private int mPositionWhenPaused = -1;

    private MediaController mMediaController;
    private String url = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         /* 设置播放视频时候不需要的部分 *//* 以下代码需要写在setContentView();之前 */
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        /* 设置全屏 */
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        /* 设置屏幕常亮 *//* flag：标记 ； */
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_video_view);

        videoView = (VideoView) findViewById(R.id.videoview);
         /* 获取MediaController对象，控制媒体播放 */
        MediaController mc = new MediaController(this);
        videoView.setMediaController(mc);
        Intent intent = getIntent();
        url = intent.getStringExtra("url");
    }

    public void onStart() {
        // Play Video
        /* 设置URI ， 指定数据 */
        videoView.setVideoURI(Uri.parse(url));

        /* 开始播放视频 */
        videoView.start();

        /*  请求获取焦点 */
        videoView.requestFocus();

        super.onStart();
    }

    public void onPause() {
        // Stop video when the activity is pause.
        mPositionWhenPaused = videoView.getCurrentPosition();
        videoView.stopPlayback();

        super.onPause();
    }

    public void onResume() {
        // Resume video player
        if (mPositionWhenPaused >= 0) {
            videoView.seekTo(mPositionWhenPaused);
            mPositionWhenPaused = -1;
        }

        super.onResume();
    }
}
